package com.wjw.service;

import com.wjw.mapper.MapTableMapper;
import com.wjw.mapper.PavementDamageTableMapper;
import com.wjw.model.Facility_damage_table;
import com.wjw.model.Map_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Facility_damage_table_view;
import com.wjw.viewmodel.Map_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MapTableService {

    //private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
    //private MapTableMapper mapper=sqlsession.getMapper(MapTableMapper.class);

    public List<Map_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        MapTableMapper mapper=sqlsession.getMapper(MapTableMapper.class);
        try {
            List<Map_table> all = mapper.findAll();
            List<Map_table_view> list= new ArrayList<Map_table_view>();
            for(Map_table e:all){
                Map_table_view k = new Map_table_view();
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setEnd_j(e.getEnd_j());
                k.setEnd_w(e.getEnd_w());
                k.setMap_id(e.getMap_id());
                k.setStart_j(e.getStart_j());
                k.setStart_w(e.getStart_w());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Map_table_view> findByPage(int page,int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        MapTableMapper mapper=sqlsession.getMapper(MapTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Map_table> all = mapper.findAll();
            List<Map_table_view> list= new ArrayList<Map_table_view>();
            for(Map_table e:all){
                Map_table_view k = new Map_table_view();
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                k.setEnd_j(e.getEnd_j());
                k.setEnd_w(e.getEnd_w());
                k.setMap_id(e.getMap_id());
                k.setStart_j(e.getStart_j());
                k.setStart_w(e.getStart_w());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Map_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        MapTableMapper mapper=sqlsession.getMapper(MapTableMapper.class);
        try {
            Map_table k = new Map_table();
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            k.setEnd_j(e.getEnd_j());
            k.setEnd_w(e.getEnd_w());
            k.setMap_id(e.getMap_id());
            k.setStart_j(e.getStart_j());
            k.setStart_w(e.getStart_w());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        MapTableMapper mapper=sqlsession.getMapper(MapTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Map_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        MapTableMapper mapper=sqlsession.getMapper(MapTableMapper.class);
        try {
            Map_table k = new Map_table();
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            k.setEnd_j(e.getEnd_j());
            k.setEnd_w(e.getEnd_w());
            k.setMap_id(e.getMap_id());
            k.setStart_j(e.getStart_j());
            k.setStart_w(e.getStart_w());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }
}
